package com.ibl.rover.lite.client.Application.Properties;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;

import com.mobile.sync.client.Sync.HttpConfiguration;

public class ApplicationProperties {

	/**
	 * format represents the type of data transfer i.e. JSON, XML etc
	 */
	public static String format = "attribute.format";
	/**
	 * server_host is the host of server
	 */
	public static String server_host = "attribute.server.host";
	/**
	 * server_port is the port of server
	 */
	public static String server_port = "attribute.server.port";

	/**
	 * server_port is the port of server
	 */
	public static String server_proxy = "server.proxy";

	/**
	 * server_port is the port of server
	 */
	public static String server_proxy_port = "server.proxy.port";
	/**
	 * server_url is the url of server
	 */
	public static String server_url = "attribute.server.url";
	/**
	 * max_transfer_size is the maximum bytes that needs to be transfer at one
	 * time
	 */
	public static String max_transfer_size = "attribute.max.transfer.size";
	/**
	 * number_of_attempts is the successive failure attempts
	 */
	public static String number_of_attempts = "attribute.number.attempts";
	/**
	 * syncin_url is the whole url for synin
	 */
	public static String syncin_url = "attribute.syncin.url";
	/**
	 * syncout_url is the whole url for syncout
	 */
	public static String syncout_url = "attribute.syncout.url";
	/**
	 * cancel_url is the whole url for syncout
	 */
	public static String cancel_url = "attribute.cancel.url";
	/**
	 * datastore_type is the type of datasource
	 */
	public static String datastore_type = "attribute.datasource.type";
	/**
     * 
     */
	public static String jsonStringLength = "attribute.string.max.length";
	/**
     *
     */
	public static String success_url = "attribute.success.url";
	/**
     *
     */
	public static String maximum_list = "attribute.maximum.list";
	/**
	 * 
     * 
     */
	public static String filePath = "file.path";

	/**
     * 
     */
	public static int LogLevel;
	public static String DEBUG_DATE_FORMAT = "rover.lite.debug.date.format";

	public static String ROVER_LOGGING_PATH = "rover.logging.path";
	private HttpConfiguration httpConfiguration=new HttpConfiguration();
	private boolean initialized = false;
	private static ApplicationProperties applicationProperties = new ApplicationProperties();
	private Properties properties = new Properties();

	public ApplicationProperties() {

	}

	public Properties getProperties() {
		return properties;
	}

	public void setProperties(Properties properties) {
		this.properties = properties;
	}

	public static ApplicationProperties getInstance(InputStream stream) {
		try {
			if (!applicationProperties.isInitialized()) {
				applicationProperties.init(stream);
			}
			stream.close();
			return applicationProperties;
		} catch (Exception ex) {
			// change it
			throw new IllegalAccessError();
		}
	}

	private boolean isInitialized() {
		return initialized;
	}

	String baseUrl, serverPort, serverProxy, proxyPort, jsonformat,
			maxTransferSize, syncOutUrl, syncInUrl, sucessUrl, cancelUrl,
			numberOfAttempt, maxLegth, maxListlength, debugDateFormat,
			loggingPath,serverUrlPath;

	private void init(InputStream stream) throws Exception {
		properties.loadFromXML(stream);
		baseUrl = properties.getProperty(server_host);
		serverPort = properties.getProperty(server_port);
		serverProxy = properties.getProperty(server_proxy);
		proxyPort = properties.getProperty(server_proxy_port);
		jsonformat = properties.getProperty(format);
		maxTransferSize = properties.getProperty(max_transfer_size);
		syncOutUrl = properties.getProperty(syncout_url);
		syncInUrl = properties.getProperty(syncin_url);
		sucessUrl = properties.getProperty(success_url);
		cancelUrl = properties.getProperty(cancel_url);
		numberOfAttempt = properties.getProperty(number_of_attempts);
		maxLegth = properties.getProperty(jsonStringLength);
		maxListlength = properties.getProperty(maximum_list);
		debugDateFormat = properties.getProperty(DEBUG_DATE_FORMAT);
		loggingPath = properties.getProperty(ROVER_LOGGING_PATH);
		serverUrlPath=properties.getProperty(server_url);
		
		
		httpConfiguration.setServerPort(Integer.parseInt(serverPort));
		httpConfiguration.setServerUrl(baseUrl);

		initialized = true;
	}

 

	public String getServerUrlPath() {
		return serverUrlPath;
	}

	public void setServerUrlPath(String serverUrlPath) {
		this.serverUrlPath = serverUrlPath;
	}

	public HttpConfiguration getHttpConfiguration() {
		return httpConfiguration;
	}

	public void setHttpConfiguration(HttpConfiguration httpConfiguration) {
		this.httpConfiguration = httpConfiguration;
	}

	public String getServerPort() {
		return serverPort;
	}

	public void setServerPort(String serverPort) {
		this.serverPort = serverPort;
	}

	public String getServerProxy() {
		return serverProxy;
	}

	public String getLoggingPath() {
		return loggingPath;
	}

	public void setLoggingPath(String loggingPath) {
		this.loggingPath = loggingPath;
	}

	public void setServerProxy(String serverProxy) {
		this.serverProxy = serverProxy;
	}

	public String getProxyPort() {
		return proxyPort;
	}

	public void setProxyPort(String proxyPort) {
		this.proxyPort = proxyPort;
	}

	public String getJsonformat() {
		return jsonformat;
	}

	public void setJsonformat(String jsonformat) {
		this.jsonformat = jsonformat;
	}

	public String getMaxTransferSize() {
		return maxTransferSize;
	}

	public void setMaxTransferSize(String maxTransferSize) {
		this.maxTransferSize = maxTransferSize;
	}

	public String getSyncOutUrl() {
		return syncOutUrl;
	}

	public void setSyncOutUrl(String syncOutUrl) {
		this.syncOutUrl = syncOutUrl;
	}

	public String getSyncInUrl() {
		return syncInUrl;
	}

	public void setSyncInUrl(String syncInUrl) {
		this.syncInUrl = syncInUrl;
	}

	public String getSucessUrl() {
		return sucessUrl;
	}

	public void setSucessUrl(String sucessUrl) {
		this.sucessUrl = sucessUrl;
	}

	public String getNumberOfAttempt() {
		return numberOfAttempt;
	}

	public void setNumberOfAttempt(String numberOfAttempt) {
		this.numberOfAttempt = numberOfAttempt;
	}

	public String getMaxLegth() {
		return maxLegth;
	}

	public void setMaxLegth(String maxLegth) {
		this.maxLegth = maxLegth;
	}

	public String getMaxListlength() {
		return maxListlength;
	}

	public void setMaxListlength(String maxListlength) {
		this.maxListlength = maxListlength;
	}

	public String getDebugDateFormat() {
		return debugDateFormat;
	}

	public void setDebugDateFormat(String debugDateFormat) {
		this.debugDateFormat = debugDateFormat;
	}

	public String getBaseUrl() {
		return baseUrl;
	}

	public void setBaseUrl(String baseUrl) {
		this.baseUrl = baseUrl;
	}

	public String getCancelUrl() {
		return cancelUrl;
	}

	public void setCancelUrl(String cancelUrl) {
		this.cancelUrl = cancelUrl;
	}

}
